package com.nnnu.wsnackshop.controller.user;

import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.constant.StatusCodeConstants;
import com.nnnu.wsnackshop.pojo.dto.StoreAddressPageDTO;
import com.nnnu.wsnackshop.pojo.vo.StoreAddressVO;
import com.nnnu.wsnackshop.result.PageResult;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IStoreAddressesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户端-门店地址查询接口
 * </p>
 *
 * @author zk
 * @since 2025-05-19
 */
@RestController("userStoreAddressesController")
@RequestMapping("/api/user/store-addresses")
@Slf4j
@Validated
@Tag(name = "用户端-门店地址接口")
@RequiredArgsConstructor
public class StoreAddressesController {

    private final IStoreAddressesService addressService;

    @Operation(summary = "分页查询门店地址", description = "分页查询门店地址信息")
    @GetMapping("/page")
    public Result<PageResult<StoreAddressVO>> pageAddresses(
            @Valid @ParameterObject StoreAddressPageDTO dto) {
        log.info("[用户端] 分页查询门店地址：{}", dto);
        PageResult<StoreAddressVO> result = addressService.pageAddresses(dto);
        return Result.success(result, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "查询指定门店所有地址", description = "查询指定门店的所有地址信息")
    @GetMapping("/list/{storeId}")
    public Result<List<StoreAddressVO>> listByStore(
            @Parameter(description = "门店ID", in = ParameterIn.PATH) @PathVariable @Min(1) Long storeId) {
        log.info("[用户端] 查询门店({})地址列表", storeId);
        List<StoreAddressVO> list = addressService.listByStore(storeId);
        return Result.success(list, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "获取门店默认地址", description = "获取指定门店的默认地址")
    @GetMapping("/default/{storeId}")
    public Result<StoreAddressVO> getDefaultAddress(
            @Parameter(description = "门店ID", in = ParameterIn.PATH) @PathVariable @Min(1) Long storeId) {
        log.info("[用户端] 查询门店({})默认地址", storeId);
        StoreAddressVO defaultAddress = addressService.getDefaultAddress(storeId);
        if (defaultAddress == null) {
            return Result.error(StatusCodeConstants.NOT_FOUND, "该门店暂无默认地址");
        }
        return Result.success(defaultAddress, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "获取门店地址详情", description = "根据地址ID获取门店地址详情")
    @GetMapping("/{id}")
    public Result<StoreAddressVO> getAddressDetail(
            @Parameter(description = "地址ID", in = ParameterIn.PATH) @PathVariable @Min(1) Long id) {
        log.info("[用户端] 查询门店地址详情：{}", id);
        StoreAddressVO address = addressService.getAddressDetail(id);
        if (address == null) {
            return Result.error(StatusCodeConstants.NOT_FOUND, "门店地址不存在");
        }
        return Result.success(address, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "批量查询门店地址", description = "根据地址ID列表批量查询门店地址")
    @GetMapping("/batch")
    public Result<List<StoreAddressVO>> batchGetAddresses(
            @Parameter(description = "地址ID列表", in = ParameterIn.QUERY) @RequestParam List<@Min(1) Long> ids) {
        log.info("[用户端] 批量查询门店地址：{}", ids);
        List<StoreAddressVO> addresses = addressService.batchGetAddresses(ids);
        return Result.success(addresses, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "搜索门店地址", description = "根据关键词搜索门店地址")
    @GetMapping("/search")
    public Result<List<StoreAddressVO>> searchAddresses(
            @Parameter(description = "搜索关键词", in = ParameterIn.QUERY) @RequestParam String keyword,
            @Parameter(description = "门店ID（可选）", in = ParameterIn.QUERY) @RequestParam(required = false) Long storeId) {
        log.info("[用户端] 搜索门店地址，关键词：{}，门店ID：{}", keyword, storeId);
        List<StoreAddressVO> addresses = addressService.searchAddresses(keyword, storeId);
        return Result.success(addresses, MessageConstants.SELECT_OK);
    }
}
